package com.bos.web.action;

import com.bos.domian.base.Area;
import com.bos.domian.base.Order;
import com.bos.domian.constant.Constants;
import com.bos.web.BaseAction;
import com.crm.domain.Customer;
import com.opensymphony.xwork2.ActionContext;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.*;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.ws.rs.core.MediaType;
import java.io.IOException;

/**
 * @author misterWei
 * @create 2018年11月23号:20点20分
 * @mailbox mynameisweiyan@gmail.com
 * //客户下订单的操作
 */
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
@SuppressWarnings("all")
public class OrderAction extends BaseAction<Order> {
    //由于省市区属于Area  发送人区域
    //接收人区域  前台绑定的参数格式为  省/市/区

    private String sendAreaInfo; // 发件人省市区 信息
    private String recAreaInfo; // 收件人 省市区信息  //血的教训， 以后千万别相信自己敲出来的setter和getter

    public void setSendAreaInfo(String sendAreaInfo) {
        this.sendAreaInfo = sendAreaInfo;
    }

    public void setRecAreaInfo(String recAreaInfo) {
        this.recAreaInfo = recAreaInfo;
    }

    @Action(value = "/order_add", results = { @Result(name = "success", type = "redirect",location = "/index.html#/myhome"),@Result(name = "none",type = "redirect",location = "/index.html#/order") })
    public String add() {
        // 手动封装 Area关联
        Customer customer = (Customer) ServletActionContext.getRequest()
                .getSession().getAttribute("customer");
        if (customer==null) {
            return NONE;
        }
        Area sendArea = new Area();
        String[] sendAreaData = sendAreaInfo.split("/");
        sendArea.setProvince(sendAreaData[0]);
        sendArea.setCity(sendAreaData[1]);
        sendArea.setDistrict(sendAreaData[2]);

        Area recArea = new Area();
        String[] recAreaData = recAreaInfo.split("/");
        recArea.setProvince(recAreaData[0]);
        recArea.setCity(recAreaData[1]);
        recArea.setDistrict(recAreaData[2]);

        model.setSendArea(sendArea);
        model.setRecArea(recArea);

        // 关联当前登录客户

        model.setCustomer_id(customer.getId());

        // 调用WebService 将数据传递 bos_management系统
      WebClient.create(Constants.BOS_MANAGER_PORT  + "/services/orderService/orderAdd").type(MediaType.APPLICATION_JSON).post(model);

        System.out.println(model);
        return SUCCESS;
    } //坑  重量为 double类型 注意了
 @Action(value = "orderAdd", results = { @Result(name = "success", type = "json") })
    public String orderAdd() {
        System.out.println(model+recAreaInfo+sendAreaInfo);  //问题所在 日个狗
        ActionContext.getContext().getValueStack().push(true);
        return "success";
    }

}
